$(function()
{
    var user = $.cookie("user")
    /*获取所有已收藏的任务并插入数据 */
    function get_all_tasks()
    {
        var tr_item_model
        $.ajax({url: "../instance/collect.html",data:{"account": user},success: function(result)
        {
            tr_item_model = result
        }});
        $.post("../api/get_collected_task.php",
        {
            "account" : user
        },function(data,status)
        {
            if(data)
            {
                console.log(data)
                console.log(typeof(data))
                var json = JSON.parse(data)
                console.log(json)
                //迭代方法不对 数据对
                var template = $.templates(tr_item_model)
                var html = template.render(json)
                console.log(html)
                $("tbody").append(html)
                //删除已收藏任务的功能
                $(".btn").on("click",function()
                {
                    // alert("点击！")
                    var td = $(this).parent()
                    //同胞
                    var simalar = td.siblings()//.filter(".task_name")
                    //console.log(td)
                    //console.log(simalar)
                    var task_link = $(simalar[0]).children("a.task_name")
                    var task_name = task_link.text()
                    $.post("../api/del_collect.php",
                    {
                        "account"  : user,
                        "task_name": task_name
                    },function(data,status)
                    {
                        if(data)
                        {
                            console.warn(task_name+"已被删除")
                            $(td).parent().remove()
                            console.log(data)
                        }
                        else
                        {
                            console.warn("无数据")
                        }
                    })
                })
            }
            else
            {
                console.warn("无数据");
            }
        })
    }
    get_all_tasks()

    $("ol li").click(function()
    {
        $(this).css("color","white ").siblings().css("color","black")
        $(this).css("background-color","#1890ff ").siblings().css("background-color","")
    })
})

function clickSwitchCollect()
{
    var audio = document.createElement("audio");
    audio.src = "./audio/se_changeitem.wav";
    audio.play();
}